﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.SysUser
{
    public partial class frmSysUser : Form
    {
        DbWrapper.DbDataTypes.User m_userInfo = null;
        public frmSysUser()
        {
            m_userInfo = new DbWrapper.DbDataTypes.User();
            InitializeComponent();
        }

        public frmSysUser(DbWrapper.DbDataTypes.User user)
        {
            m_userInfo = user;
            InitializeComponent();
        }

        private void SysUser_Load(object sender, EventArgs e)
        {
            comboBoxRoleType.DataSource = SYSUSER_DEFINE_DATA.USER_ROLE.Clone();
            Update();
        }

        public void Update(bool bView=true)
        {
            if (bView)
            {
                if (m_userInfo != null)
                {
                    textBoxUsername.Text = m_userInfo.username;

                    int nIndex = 0;
                    UserConfigItem item =
                        SYSUSER_DEFINE_DATA.find(SYSUSER_DEFINE_DATA.USER_ROLE,
                        m_userInfo.roletype,
                        out nIndex);
                    if (item != null)
                    {
                        comboBoxRoleType.SelectedIndex = nIndex;
                    }
                }
            }
            else
            {
                m_userInfo.username = textBoxUsername.Text;
                m_userInfo.password = textBoxPasswd.Text;
                UserConfigItem item = (UserConfigItem)comboBoxRoleType.SelectedValue;
                m_userInfo.roletype = (int)item.Value;
            }
        }

        public DbWrapper.DbDataTypes.User GetUserInfo()
        {
            return m_userInfo;
        }
    }
}
